package com.gla.giliair.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gla.giliair.dao.StaticSeatDao;
import com.gla.giliair.entity.*;
import com.gla.giliair.service.PlanemodelService;
import com.gla.giliair.service.StaticSeatService;
import com.gla.giliair.util.BeanUtil;
import com.gla.giliair.util.MapGenerator;
import com.gla.giliair.web.vo.PlanemodelVO;
import com.gla.giliair.web.vo.SeatVO;
import com.gla.giliair.web.vo.StaticSeatVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/28 16:18
 */
@Service
public class StaticSeatServiceImpl implements StaticSeatService {
    @Autowired
    private StaticSeatDao staticSeatDao;

    @Autowired
    private PlanemodelService planemodelService;

    @Override
    public StaticSeat selectStaticSeatById(Long staticSeatId) {
        if (staticSeatId != null) {
            StaticSeatExample staticSeatExample = new StaticSeatExample();
            staticSeatExample.or().andStaticSeatIdEqualTo(staticSeatId);
            List<StaticSeat> staticSeatList = staticSeatDao.selectByExample(staticSeatExample);
            if (staticSeatList.isEmpty()) {
                return null;
            }
            return staticSeatList.get(0);
        }
        return null;
    }

    @Override
    public List<StaticSeat> selectStaticSeatsByPlanemodelId(Long planemodelId) {
        if (planemodelId != null) {
            StaticSeatExample staticSeatExample = new StaticSeatExample();
            staticSeatExample.setOrderByClause("static_seat_id asc");
            staticSeatExample.or().andPlanemodelIdEqualTo(planemodelId);
            List<StaticSeat> staticSeatList = staticSeatDao.selectByExample(staticSeatExample);
            return staticSeatList;
        }
        return null;
    }

    @Override
    public List<StaticSeat> selectStaticSeatsByPlanemodelIdAndSeatType(Long planemodelId, Integer seatType) {
        if (planemodelId != null) {
            StaticSeatExample staticSeatExample = new StaticSeatExample();
            staticSeatExample.setOrderByClause("static_seat_id asc");
            staticSeatExample.or().andPlanemodelIdEqualTo(planemodelId).andSeatTypeEqualTo(seatType);
            List<StaticSeat> staticSeatList = staticSeatDao.selectByExample(staticSeatExample);
            return staticSeatList;
        }
        return null;
    }

    @Override
    public List<StaticSeat> selectAllStaticSeats(StaticSeatExample orderBy) {
        List<StaticSeat> planemodelList = staticSeatDao.selectByExample(orderBy);
        return planemodelList;
    }

    @Override
    public Map<String, Object> findAllStaticSeats(String sort, boolean isASC, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<StaticSeat> staticSeatList = selectAllStaticSeats(staticSeatSort(sort, isASC));
        PageInfo pageInfo = new PageInfo(staticSeatList);
        if (!staticSeatList.isEmpty()) {
            List<StaticSeatVO> staticSeatVOList = BeanUtil.copyList(staticSeatList, StaticSeatVO.class);
            pageInfo.setList(staticSeatVOList);
            return MapGenerator.getResWithData(200, "查询静态座位列表成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "查询结果为空！");
        }
    }

    @Override
    public Map<String, Object> findStaticSeatsByPlanemodelId(Long planemodelId, Integer page, Integer pageSize) {
        Planemodel planemodelFromDB = planemodelService.selectPlanemodelById(planemodelId);
        if (planemodelFromDB == null) {
            return MapGenerator.getRes(501, "飞机机型不存在！");
        }
        PageHelper.startPage(page, pageSize);
        List<StaticSeat> staticSeatList = selectStaticSeatsByPlanemodelId(planemodelId);
        PageInfo pageInfo = new PageInfo(staticSeatList);

        if (!staticSeatList.isEmpty()) {
            List<StaticSeatVO> staticSeatVOList = BeanUtil.copyList(staticSeatList, StaticSeatVO.class);
            pageInfo.setList(staticSeatVOList);
            return MapGenerator.getResWithData(200, "获取静态座位列表成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "获取结果为空！");
        }
    }

    @Override
    public Map<String, Object> addStaticSeats(Long planemodelId, Integer seatType, Integer[] numPerRow, Integer rowNum) {
        Planemodel planemodelFromDB = planemodelService.selectPlanemodelById(planemodelId);
        if (planemodelFromDB == null) {
            return MapGenerator.getRes(501, "飞机机型不存在！");
        }
        if (numPerRow.length != 2 && numPerRow.length != 3) {
            return MapGenerator.getRes(502, "每行每区数不为2或3，区数非法！");
        }
        Integer empty = 12;
        for (Integer numPerArea : numPerRow) {
            empty -= numPerArea;
        }
        if (empty < 0) {
            return MapGenerator.getRes(503, "每行座位数总数大于12，座位总数非法！");
        }
        List<StaticSeat> staticSeatList = selectStaticSeatsByPlanemodelIdAndSeatType(planemodelId, seatType);
        Integer totality = getStaticSeatTotality(planemodelFromDB, seatType);
        Integer count = (12 - empty) * rowNum;
        if (count + staticSeatList.size() > totality) {
            return MapGenerator.getRes(504, "插入座位数量超出了机型舱位总数！");
        }

        //index为开始位
        Integer index = getIndex(planemodelId);
        for (int i = index + 1; i <= index + rowNum; i++) {
            addStaticSeatRow(planemodelId, seatType, i, numPerRow, empty);
        }
        return MapGenerator.getRes(200, "批量添加静态座位成功！");
    }

    @Override
    public Map<String, Object> deleteStaticSeat(Long staticSeatId) {
        StaticSeat staticSeatFromDB = selectStaticSeatById(staticSeatId);
        if (staticSeatFromDB == null) {
            return MapGenerator.getRes(501, "静态座位不存在！");
        }
        if (staticSeatDao.deleteByPrimaryKey(staticSeatId) > 0) {
            return MapGenerator.getRes(200, "删除静态座位成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    //排序参数转化
    private StaticSeatExample staticSeatSort(String sort, boolean isASC) {
        StaticSeatExample staticSeatExample = new StaticSeatExample();
        sort = sort == null ? "" : sort;
        String column = "";
        String order = "desc";
        if (isASC) {
            order = "asc";
        }
        switch (sort) {
            case "staticSeatId":
                column = "static_seat_id";
                break;
            case "seatNumber":
                column = "seat_number";
                break;
            case "seatType":
                column = "seat_type";
                break;
            case "planemodelId":
                column = "planemodel_id";
                break;
            case "createTime":
                column = "create_time";
                break;
            case "updateTime":
                column = "update_time";
                break;
            default:
        }
        if ("".equals(column)) {
            staticSeatExample.setOrderByClause("static_seat_id desc");
        } else {
            if ("company_id".equals(column)) {
                staticSeatExample.setOrderByClause(String.format("%s %s", column, order));
            } else {
                staticSeatExample.setOrderByClause(String.format("%s %s , static_seat_id desc", column, order));
            }
        }
        return staticSeatExample;
    }

    //数字转化成字母
    private String numToStr(Integer num) {
        String str = "";
        switch (num) {
            case 1:
                str = "A";
                break;
            case 2:
                str = "B";
                break;
            case 3:
                str = "C";
                break;
            case 4:
                str = "D";
                break;
            case 5:
                str = "E";
                break;
            case 6:
                str = "F";
                break;
            case 7:
                str = "G";
                break;
            case 8:
                str = "H";
                break;
            case 9:
                str = "I";
                break;
            case 10:
                str = "J";
                break;
            case 11:
                str = "K";
                break;
            case 12:
                str = "L";
                break;
            default:
        }
        return str;
    }

    private boolean addStaticSeatRow(Long planemodelId, Integer seatType, Integer rowNum, Integer[] numPerRow, Integer empty) {
        Integer areaNum = numPerRow.length;
        if (areaNum == 2) {
            for (int i = 0; i < numPerRow[0]; i++) {
                StaticSeat staticSeat = getCommonStaticSeat(planemodelId, seatType);
                staticSeat.setSeatNumber(rowNum + numToStr(i + 1));
                if (staticSeatDao.insertSelective(staticSeat) <= 0) {
                    return false;
                }
            }
            for (int i = 0; i < numPerRow[1]; i++) {
                StaticSeat staticSeat = getCommonStaticSeat(planemodelId, seatType);
                staticSeat.setSeatNumber(rowNum + numToStr(12 - i));
                if (staticSeatDao.insertSelective(staticSeat) <= 0) {
                    return false;
                }
            }
        }
        if (areaNum == 3) {
            for (int i = 0; i < numPerRow[0]; i++) {
                StaticSeat staticSeat = getCommonStaticSeat(planemodelId, seatType);
                staticSeat.setSeatNumber(rowNum + numToStr(i + 1));
                if (staticSeatDao.insertSelective(staticSeat) <= 0) {
                    return false;
                }
            }
            for (int i = 0; i < numPerRow[1]; i++) {
                StaticSeat staticSeat = getCommonStaticSeat(planemodelId, seatType);
                staticSeat.setSeatNumber(rowNum + numToStr(numPerRow[0] + empty / 2 + 1 + i));
                if (staticSeatDao.insertSelective(staticSeat) <= 0) {
                    return false;
                }
            }
            for (int i = 0; i < numPerRow[2]; i++) {
                StaticSeat staticSeat = getCommonStaticSeat(planemodelId, seatType);
                staticSeat.setSeatNumber(rowNum + numToStr(numPerRow[0] + numPerRow[1] + empty + 1 + i));
                if (staticSeatDao.insertSelective(staticSeat) <= 0) {
                    return false;
                }
            }
        }
        return true;
    }

    private StaticSeat getCommonStaticSeat(Long planemodelId, Integer seatType) {
        StaticSeat staticSeat = new StaticSeat();
        staticSeat.setPlanemodelId(planemodelId);
        staticSeat.setSeatType(seatType);
        Date nowDate = new Date();
        staticSeat.setCreateTime(nowDate);
        staticSeat.setUpdateTime(nowDate);
        return staticSeat;
    }

    private Integer getStaticSeatTotality(Planemodel planemodelFromDB, Integer seatType) {
        Integer totality = 0;
        if (seatType == 0) {
            totality = planemodelFromDB.getEconomyClassTotality();
        }
        if (seatType == 1) {
            totality = planemodelFromDB.getBusinessClassTotality();
        }
        if (seatType == 2) {
            totality = planemodelFromDB.getFirstClassTotality();
        }
        return totality;
    }

    private Integer getIndex(Long planemodelId) {
        List<StaticSeat> staticSeatList = selectStaticSeatsByPlanemodelId(planemodelId);
        Integer index = 0;
        if (!staticSeatList.isEmpty()) {
            for (StaticSeat staticSeat : staticSeatList) {
                String seatNumber = staticSeat.getSeatNumber();
                Integer row = Integer.valueOf(seatNumber.substring(0, seatNumber.length() - 1));
                if (row > index) {
                    index = row;
                }
            }
        }
        return index;
    }
}
